package com.macrounion.atv.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class ReadThread implements Runnable{
	private String charset;
	private InputStream is;
	private List<String> resultList;
	
	public ReadThread (InputStream is,String charset,List<String> resultList){
		this.is = is;
		this.charset = charset;
		this.resultList = resultList;
	}
	
	public ReadThread (InputStream is,List<String> resultList){
		this(is, "GBK", resultList);
	}
	
	public void start(){
		Thread thread = new Thread(this);
		thread.setDaemon(true);
		thread.start();
	}

	@Override
	public void run() {
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new InputStreamReader(is, charset));
			String line = null;
			while((line = br.readLine())!=null){
				resultList.add(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			
			try {
				if(is!=null)
					is.close();
				
				if(br!=null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
